package com.tjl.dao;

import com.mysql.jdbc.JDBC4Connection;
import com.tjl.bean.User;
import com.tjl.jdbc.JDBC;
import com.tjl.jdbc.JDBCUtils;

import java.sql.*;

public class UserDao_Imp  implements UserDao{
    private static final String SQL_USER_LOGIN = "SELECT type from user WHERE uname=? AND upass =?";
    private static final String SQL_USER_INSERT= "INSERT user(id, uname, upass, type)"+"VALUES (id, ?, ?, 2)";
    private static final String SQL_USER_DELETE= "DELETE FROM user WHERE uname = ?";
    private static final String SQL_USER_UPDATE= "UPDATE user SET upass = ? WHERE uname = ?";
    private static final String SQL_USER_SELECT= "SELECT * FROM user WHERE uname = ?";

    @Override
    public int login(User user) {
        //连接数据库，创建连接对象conn
         Connection conn = JDBCUtils.getConnection();
         PreparedStatement prepareStatement = null;
         ResultSet result = null;
         try{
             //创建与编译环境
             prepareStatement = conn.prepareStatement(SQL_USER_LOGIN);
             //设置sql语句中的参数
             prepareStatement.setString(1, user.getUname());
             prepareStatement.setString(2, user.getUpass());
             //执行语句
             result = prepareStatement.executeQuery();
             while(result.next()){
                 //用结果查询当前用户权限
                 int type = result.getInt("type");
                 return type;
             }
         }catch(SQLException e){
             //TODO Auto-generated catch block
             e.printStackTrace();
         }finally {
             JDBCUtils.close(conn, prepareStatement, result);
         }
        return -1;
    }

    @Override
    public boolean insert(User user) {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement prepareStatement = null;
        try{
            prepareStatement = conn.prepareStatement(SQL_USER_INSERT);
            prepareStatement.setString(1,user.getUname());
            prepareStatement.setString(2,user.getUpass());
            int  result = prepareStatement.executeUpdate();
            return  result >0 ? true : false;
        }catch (SQLException se){
            se.printStackTrace();
        }finally {
            JDBCUtils.close(conn, prepareStatement, null);
        }
        return false;
    }

    @Override
    public boolean delete(String uname) {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement = conn.prepareStatement(SQL_USER_DELETE);
            preparedStatement.setString(1, uname );
            int result = preparedStatement.executeUpdate();
            return result > 0 ? true : false;
        }catch (SQLException se){
            se.printStackTrace();
        }finally {
            JDBCUtils.close(conn, preparedStatement, null);
        }
        return false;
    }

    @Override
    public boolean update(User user) {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        try{
            ps = conn.prepareStatement(SQL_USER_UPDATE);
            ps.setString(1, user.getUpass());
            ps.setString(2, user.getUname());
            int result = ps.executeUpdate();
            return result>0?true:false;
        }catch (SQLException se){
            se.printStackTrace();
        }finally {
            JDBCUtils.close(conn, ps, null);
        }
        return false;
    }

    @Override
    public User select(String uname) {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet result = null;
        try{
            preparedStatement = conn.prepareStatement(SQL_USER_SELECT);
            preparedStatement.setString(1, uname );
            result = preparedStatement.executeQuery();
            while(result.next()){
                int user_id = result.getInt("id");
                String user_name = result.getString("uname");
                String user_pass = result.getString("upass");
                int user_type = result.getInt("type");
                return new User(user_id, user_name, user_pass, user_type);
            }
        }catch (SQLException se){
            se.printStackTrace();
        }finally {
            JDBCUtils.close(conn, preparedStatement, null);
        }
        return null;
    }
}
